#!/bin/sh
#
# Usage:
#   gnc-svnversion <srcdir>
#
# Prints the revision number to stdout and exits 0 on success
# exits with errorcode 1 if we're not in an svn, svk or git checkout
#
# Written By:  Derek Atkins <derek@ihtfp.com>
#
# $Id$

# Print an error message and then exit
my_die()
{
  echo "$1"
  exit 1
}

if [ "$BUILDING_FROM_SVN" = "0" -o \
     "$BUILDING_FROM_SVN" = "no" -o \
     "$BUILDING_FROM_SVN" = "false" ]; then
  # Something outside of the Gnucash build system knows that we
  # are NOT building from svn, svk or git checkout.
  # We should believe it (needed by packagers that maintain the
  # packaging code in svn, svk or git too)
  echo "Environment variable BUILDING_FROM_SVN=$BUILDING_FROM_SVN" >&2
  exit 1
fi

# Make sure we have a srcdir
[ -n "$1" ] || my_die "Usage: $0 <srcdir>"
[ -d "$1" ] || my_die "$0: $1: not a directory"

# Find the real srcdir.
# This handles the case of a symlink (lndir) tree
# $real_srcdir will hold the actual source dir
if test -h "$1"/Makefile.am
then 
  tmp_srcdir=`readlink "$1"/Makefile.am`
  real_srcdir="$1/`dirname ${tmp_srcdir}`"
else
  real_srcdir="$1"
fi

# Test if this code is an SVN Checkout
#   If this is an svn checkout we assume you have svnversion!
if test -d "${real_srcdir}"/.svn
then
  # svnversion without options returns the most recent revision in the repo
  # at the time of the last svn update/checkout even if that revision
  # didn't hold any changes in the current path. Not very useful as a
  # reference. Better is to ask for the last changed revision with -c.
  # BUT: -c option for some reason always considers svn working copy
  # as having mixed sources. We're only interested in the last part so
  # we strip the beginning revision if it's there.
  # As a result if your WC really has mixed sources, that will not be
  # visible in the version number...
  svn_mixed_version=$(svnversion -c "$real_srcdir")
  echo ${svn_mixed_version#*:}
  exit $?
fi

# If we get here then this is NOT an svn checkout.

# Maybe it's git?
real_gitdir="${real_srcdir}"/.git
if test -d "${real_gitdir}"
then
  githead=`git --git-dir "${real_gitdir}" log -1 --pretty=format:"%h" HEAD 2>/dev/null`  # short hash only
  if test $? = 0 ; then
    /bin/echo -n $githead
    # Add a "+" to the hash if there deleted or modified files (not excluded by .gitignore and friends)
    # "Ignores" untracked files
    # [ $(git --git-dir "${real_gitdir}" ls-files -dkm 2>/dev/null | wc -l) -gt 0 ] && /bin/echo -n "+"
    # Includes untracked files
    [ $(git --git-dir "${real_gitdir}" ls-files -dkmo --exclude-standard 2>/dev/null | wc -l) -gt 0 ] && /bin/echo -n "+"
    echo
    exit 0
  else
    exit 1
  fi
fi

if test -d "${real_srcdir}"/.bzr ;
then
    bzrhead=`(cd "${real_srcdir}"; bzr version-info --custom --template='{revno}\n')`
    if test $? = 0 ; then
	echo "$bzrhead";
	exit 0
    else
	exit 1
    fi
fi

if test $OSTYPE -a $OSTYPE = "msys";
then
  svk_name="svk.bat"
  svk_cmd="cmd \/c svk"
else
  svk_name="svk"
  svk_cmd="svk"
fi

# Maybe it's SVK?  First, check if we've got 'svk' in the path.  If not,
# then exit with an error code of 1..
which $svk_name >/dev/null 2>&1
if test $? != 0 ; then exit 1 ; fi

# Okay, we have 'svk'.  Now see if $real_srcdir is an svk checkout
# Note that the 'echo n' is to protect against having svk installed
# without having configured the depotmap.
svkinfo=`echo n | $svk_cmd info "$real_srcdir" 2>&1`
if test $? != 0 ; then exit 1 ; fi

# If we got here, then $real_srcdir is an svk checkout.  Parse out
# the revision info, print it out, and then output 0.  Just combine
# all the revision numbers into a single string by combining them
# with periods.
svkinfo=`$svk_cmd info "$real_srcdir" | grep Rev | head -5 | sed -e 's/^.* \([0-9:]*\)$/\1/'`
echo $svkinfo | sed -e 's/ /./g'

exit 0
